package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator ossFileUploadOperator;
    /*@PostMapping("/upload")
    public Result upload(String name , Integer  age , MultipartFile  file) throws Exception {
        log.info("name:{},age:{},file:{}",name,age,file);
        //1.构造一文件名称（不重复）
        String fileName = file.getOriginalFilename();//获取原始文件名称
        String extName = fileName.substring(fileName.lastIndexOf("."));//截取文件.后缀
        String newFileName = UUID.randomUUID().toString() + extName;//生成新的文件名称
        //2.将文件保存到磁盘中------->指定的文件中
        file.transferTo(new File("E:\\images\\"+ newFileName));
        return Result.success();
    }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传开始:{}",image.getOriginalFilename());
        //1.构造一文件名称（不重复）
        String fileName = image.getOriginalFilename();//获取原始文件名称
        String extName = fileName.substring(fileName.lastIndexOf("."));//截取文件.后缀
        String newFileName = UUID.randomUUID().toString() + extName;//生成新的文件名称
        String url = ossFileUploadOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }

}
